#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from Logger import logger
import time


def timer(tracer=True):
    """计时装饰器"""
    def _timer(func):
        alltime = 0

        def wrapper(*args, **kwargs):
            if tracer:
                start = time.clock()
                result = func(*args, **kwargs)
                elapsed = time.clock() - start
                nonlocal alltime
                alltime += elapsed
                logger.debug('%s 耗时 %.5f 秒, 总计耗时 %.5f 秒' % (func.__name__, elapsed, alltime))
                return result
            else:
                return func(*args, **kwargs)
        return wrapper
    return _timer
